package com.hatricks.is.hutool;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.StopWatch;
import cn.hutool.core.io.unit.DataSizeUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @packageName: com.hatricks.is.hutool
 * @className: HutoolTest
 * @author: yanyuhui y568821795@outlook.com
 * @date: 2025-05-14 18:43
 * @description:
 */
public class HutoolTest {
    public static void main(String[] args) {
        // 1. 将文件大小转换为可视化大小
        //fileDataSize();
        // 2. 将耗时转换为人可读的时间
        //timeFormat();
        // 3. 判断两个数字是否相等
        //sum=857.0603468, origin=857.060347
        //System.out.println(NumberUtil.equals(new BigDecimal("857.060347").setScale(5, RoundingMode.HALF_UP), new BigDecimal("857.0603468").setScale(5, RoundingMode.HALF_UP)));
    }

    private static void timeFormat() {
        StopWatch sw = new StopWatch();
        sw.start();
        ThreadUtil.safeSleep(RandomUtil.randomInt(100, 8000));
        sw.stop();
        System.out.println(StrUtil.format("任务耗时:[{}]", DateUtil.formatBetween(sw.getTotalTimeMillis())));
    }

    private static void fileDataSize() {
        long size1 = 500;          // 小于 1 KB
        long size2 = 1500;         // 1.5 KB
        long size3 = 1024 * 1024 * 5; // 5 MB
        long size4 = 1024L * 1024 * 1024 * 2; // 2 GB
        long size5 = 1024L * 1024 * 1024 * 1024 * 3; // 3 TB

        String formattedSize1 = DataSizeUtil.format(size1);
        String formattedSize2 = DataSizeUtil.format(size2);
        String formattedSize3 = DataSizeUtil.format(size3);
        String formattedSize4 = DataSizeUtil.format(size4);
        String formattedSize5 = DataSizeUtil.format(size5);

        System.out.println(size1 + " bytes -> " + formattedSize1);
        System.out.println(size2 + " bytes -> " + formattedSize2);
        System.out.println(size3 + " bytes -> " + formattedSize3);
        System.out.println(size4 + " bytes -> " + formattedSize4);
        System.out.println(size5 + " bytes -> " + formattedSize5);
    }
}
